#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define N 10

typedef void (*callback_t)();                       // 函数指针

void Worker()
{
    int cnt = 10;
    while (cnt)
    {
        printf("I am child process, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt);
        sleep(1);
        cnt--;
    }
}

void createSubPorcess(int n, callback_t cb)
{
    int i = 0;
    for (i = 0; i < n; i++)
    {
        sleep(1);
        pid_t id = fork();
        if (id == 0)
        {
            printf("create child process success: %d\n", i);
            // child
            cb();
            exit(0);
        }
    }
}

//__StartCTR();

int main()
{

    createSubPorcess(N, Worker);
    // 只有父进程走到这里
    sleep(100);
    return 0; 
}